package com.example.controller;

import com.example.common.Result;
import com.example.entity.ZyzActivity;
import com.example.mapper.ZyzActivityMapper;
import com.example.service.ZyzActivityService;
import com.example.service.ZyzStudentService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hpsf.Decimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 志愿活动控制器
 */

@RestController
@RequestMapping("/api/zyzActivity")
public class ZyzActivityController {
    @Autowired
    private ZyzActivityService zyzActivityService;
    @Autowired
    private ZyzStudentService zyzStudentService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody ZyzActivity zyzActivity) {
        zyzActivityService.add(zyzActivity);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @PostMapping("/delete/batch")
    public Result deleteBatch(String ids) {
        int i = zyzActivityService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody ZyzActivity zyzActivity) {
        zyzActivityService.updateById(zyzActivity);
        return Result.success();
    }

    /**
     * 根据id查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        ZyzActivity zyzActivity = zyzActivityService.selectById(id);
        return Result.success(zyzActivity);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(ZyzActivity zyzActivity) {
        List<ZyzActivity> list =zyzActivityService.selectAll(zyzActivity);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(ZyzActivity zyzActivity,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<ZyzActivity> page = zyzActivityService.selectPage(zyzActivity, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 查询志愿者人数、志愿时长、总活动数
     */
    @GetMapping("/selectHome")
    public Result selectHome() {
        Integer studentCount = zyzStudentService.selectCount();
        Integer activityCount = zyzActivityService.selectCount();
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put("studentCount", studentCount);
        stringIntegerHashMap.put("activityCount", activityCount);
        stringIntegerHashMap.put("timeCount",activityCount * 3);
        return Result.success(stringIntegerHashMap);
    }

    /**
     * 查询学院志愿者时长排名
     */
    @GetMapping("/selectjoin")
    public Result selectjoin(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Map<String, Object>> list = zyzActivityService.selectCollegeCount(pageNum,pageSize);
        return Result.success(list);
    }

    @Autowired
    private ZyzActivityMapper zyzActivityMapper;
    /**
     * 查询每个
     */
    @GetMapping("/selectAverage")
    public Result selectAverage() {
        List<Map<String, Object>> list = zyzActivityService.selectCollegeCount2();
        double sum = 0.0;
        for (Map<String, Object> map : list) {
            Object canyulv = map.get("canyulv");
            sum += Double.parseDouble(canyulv.toString());
        }
        BigDecimal decimal = new BigDecimal(sum);
        BigDecimal bigDecimal = new BigDecimal(list.size());
        BigDecimal divide = decimal.divide(bigDecimal, 2, BigDecimal.ROUND_HALF_UP);
//        sum = sum / list.size();
        HashMap<String, Object> map = new HashMap<>();
        map.put("学院志愿者平均参与率", divide);
        return Result.success(map);
    }
}
